/**
 * 
 */
package nl.boektrust.btsoep.client;

import java.util.Date;

import com.google.gwt.event.dom.client.BlurEvent;
import com.google.gwt.event.dom.client.BlurHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * @author rao3
 *
 */
public  class EditBookGeillustreerdPanel extends EditBookPanel implements ClickHandler, ValueChangeHandler, KeyPressHandler {
	private boolean geillustreerd;
	private int numBWImages = -1;
	private int numColorImages = -1;
	private CheckBox geillustreerdCheckBox = null;
	private TextBox kleurTextBox;
	private TextBox zwTextBox;
	private Button xButton = new Button("x");;
	int numFocusedElements = 1;
	private EditBookDialog parentDialog;

	public EditBookGeillustreerdPanel(String label, boolean geillustreerd, int numBWImages, int numColorImages, boolean isInEditMode, EditBookDialog parentDialog) {
		super(label, isInEditMode);
		this.geillustreerd = geillustreerd;
		this.numBWImages = numBWImages;
		this.numColorImages = numColorImages;
		geillustreerdCheckBox = new CheckBox("Geillustreerd ");
		editPanel.add(geillustreerdCheckBox);
		editPanel.add(new HTML("<i># kleur: </i>"));
		kleurTextBox = new TextBox();
		kleurTextBox.addKeyPressHandler(this);
		kleurTextBox.setEnabled(geillustreerd);
		kleurTextBox.setSize("5em", "1em");
		editPanel.add(kleurTextBox);
		editPanel.add(new HTML("<i># z/w: </i>"));
		this.parentDialog = parentDialog;

		zwTextBox = new TextBox();
		zwTextBox.setEnabled(geillustreerd);
		zwTextBox.addKeyPressHandler(this);
		zwTextBox.setSize("5em", "1em");
		editPanel.add(zwTextBox);
		editPanel.add(xButton);
		geillustreerdCheckBox.addValueChangeHandler(this);
		kleurTextBox.addValueChangeHandler(this);
		zwTextBox.addValueChangeHandler(this);
		xButton.addClickHandler(this);

		if (isInEditMode == true) {
			setToEditMode();
		} else {
			setToDisplayMode();
		}
	}
	
	public EditBookGeillustreerdPanel(String label, boolean isInEditMode, EditBookDialog parentDialog) {
		this(label, false, -1, -1, isInEditMode, parentDialog);
	}
	
	public EditBookGeillustreerdPanel(boolean isInEditMode, EditBookDialog parentDialog) {
		this("", isInEditMode, parentDialog);
	}
	
	public EditBookGeillustreerdPanel(String label, boolean isInEditMode) {
		this(label, isInEditMode, null);
	}

	public EditBookGeillustreerdPanel(String label, EditBookDialog parentDialog) {
		this(label, false, parentDialog);
	}


	public EditBookGeillustreerdPanel(boolean isInEditMode) {
		this("", isInEditMode);
	}

	public EditBookGeillustreerdPanel(String label) {
		this(label, false);
	}

	public EditBookGeillustreerdPanel() {
		this(false);
	}
	

	


	boolean hasInvalidData() {
		return false;
	}


	/**
	 * @return the geillustreerd
	 */
	public boolean isGeillustreerd() {
		return geillustreerd;
	}

	/**
	 * @param geillustreerd the geillustreerd to set
	 */
	public void setGeillustreerd(boolean geillustreerd) {
		this.geillustreerd = geillustreerd;
	}

	/**
	 * @return the numBWImages
	 */
	public int getNumBWImages() {
		return numBWImages;
	}

	/**
	 * @param numBWImages the numBWImages to set
	 */
	public void setNumBWImages(int numBWImages) {
		this.numBWImages = numBWImages;
	}

	/**
	 * @return the numColorImages
	 */
	public int getNumColorImages() {
		return numColorImages;
	}

	/**
	 * @param numColorImages the numColorImages to set
	 */
	public void setNumColorImages(int numColorImages) {
		this.numColorImages = numColorImages;
	}


	@Override 
	void setToEditMode() {
		super.setToEditMode();
		geillustreerdCheckBox.setValue(geillustreerd);
		if (numColorImages > -1) kleurTextBox.setText(new Integer(numColorImages).toString());
		if (numBWImages > -1) zwTextBox.setText(new Integer(numBWImages).toString());
	}

	@Override
	void setToDisplayMode() {
		super.setToDisplayMode();
		if (geillustreerd == true) {
			String string = "Geillustreerd; ";
			if (numColorImages >= 0) {
				string = string+numColorImages+" kleur; ";
			}
			if (numBWImages >= 0) {
				string = string+numBWImages+" z/w; ";
			}
			theLabel.setText(string);	
		} else {
			theLabel.setText("Niet geillustreerd");
		}		
	}
	
	public void onClick(ClickEvent event) {
		if (event.getSource() ==theLabel) {		
			this.toggleEditMode();
		} else if (event.getSource() == xButton) {
			setInEditMode(false);
			this.setToDisplayMode();
		}		
	}
	

	public void onValueChange(ValueChangeEvent event) {
		// TODO Auto-generated method stub
		if (event.getSource() == geillustreerdCheckBox) {
			geillustreerd = geillustreerdCheckBox.getValue();
			// enable the textboxes (if the checkbox is checked)
			kleurTextBox.setEnabled(geillustreerd);
			zwTextBox.setEnabled(geillustreerd);
			// reset the values, if geillustreerd is deselectd
			if (geillustreerd == false) {
				numColorImages = -1;
				numBWImages = -1;
			}
		} else if (event.getSource() == kleurTextBox) {
			if (!kleurTextBox.getText().equals("")) {
				numColorImages = new Integer(kleurTextBox.getText()).intValue();
			} else {
				numColorImages = -1;
			}
		} else if (event.getSource() == zwTextBox) {
			if (!zwTextBox.getText().equals("")) {
				numBWImages = new Integer(zwTextBox.getText()).intValue();
			} else {
				numBWImages = -1;
			}
		}
		if (parentDialog != null) parentDialog.panelHasChanged();
		
	}

	public void onKeyPress(KeyPressEvent event) {
		// TODO Auto-generated method stub
		if (event.getSource() == kleurTextBox || event.getSource() == zwTextBox) {
			if (Character.isLetter(event.getCharCode()) || Character.isSpace(event.getCharCode())) {
		          ((TextBox) event.getSource()).cancelKey();
		        }
		}
		
	}

	


}
